﻿
using Models.Common;
using Models.Device;
using System.Diagnostics;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    ///普锐森社 RS485-ec检测 10-20000
    /// </summary>
    public class ServiceDeviceRS4851044_EC10_20000
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //回调
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;


                if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                {
                    return;
                }
                if (modelDeviceData.commandResults[0].value.Substring(0, 2) != commandLogModel.content.Substring(0, 2))
                {
                    return;
                }

                //普锐森社 RS485-ec检测
                // 计算：（十六进制）转十进制
                if (commandLogModel.key == "collect")
                {

                    string result = modelDeviceData.commandResults[0].value;
                    if (result.Length != 26)
                    {
                        return;
                    }

                    string dataHex = result.Substring(9, 5);
                    double ec = new UtilsDataTypeChange().hex16Convert10(dataHex);
                    Debug.WriteLine(ec);

                    //温度
                    dataHex = result.Substring(15, 5);
                    // double temperature = new UtilsDataTypeChange().hex16Convert10(dataHex) * 0.1;


                    string temperatureBinary = UtilsDataTypeChange.hexToBinaryTolStr(dataHex);
                    double temperature = 0.0;
                    if (temperatureBinary.Substring(0, 1) == "0")
                    {
                        temperature = new UtilsDataTypeChange().hex16Convert10(dataHex) * 0.1;
                    }
                    else if (temperatureBinary.Substring(0, 1) == "1")
                    {
                        //    
                        //"FF 9B" 转二进制1111111110011011
                        //1111111110011011 反码 
                        //0000000001100100
                        //补码最后一位加1
                        //0000000001100101
                        //=1100101
                        string bumabinary = UtilsByte.GetBuMaBinary(temperatureBinary);
                        temperature = UtilsDataTypeChange.binaryToDecimalStr(bumabinary) * -0.1;
                    }


                    Debug.WriteLine(temperature.ToString() + "℃");

                    //Dictionary<string, double> keyValuePairs = JsonConvert.DeserializeObject<Dictionary<string, double>>(modelDeviceData.content);
                    //写日志
                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    log.writeSensorlog(commandLogModel.d_guid, "ec", UtilsNumber.GetNumberForLength(ec, 1).ToString(), "EC值", result);
                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(temperature, 1).ToString(), "温度", result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
